package cfgs.gestudent.dao.grup;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import cfgs.gestudent.dao.connexio.ConnexioDB;
import cfgs.gestudent.preferencies.ConfiguracionsIdiomes;

public class InserirGrup {
	
	private ConfiguracionsIdiomes idioma = new ConfiguracionsIdiomes();
	private ResourceBundle rb = idioma.getIdioma();
	
	public boolean inserirGrupCurs(int idCurs, String nomGrup){
		boolean seguir = false;
		Connection connexio = ConnexioDB.getConnexio();
		Statement st = null;
		try {
			connexio.setAutoCommit(false);
			st = connexio.createStatement();
			String query = "INSERT INTO GRUP_CURS VALUES(NULL, "+idCurs+", '"+nomGrup+"')";
			
			int  i = st.executeUpdate(query);
			
			if(i==0){
				seguir = false;
				connexio.rollback();
			}else{
				seguir = true;
				connexio.commit();
			}
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,
					rb.getString("missatgeErrorStatement") + "\n " + e,
					rb.getString("titolErrorStatement"),
					JOptionPane.ERROR_MESSAGE);
		}finally {
			try {
				connexio.close();
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
				JOptionPane
						.showMessageDialog(null,
								rb.getString("missatgeErrorTancarConnexio")
										+ "\n " + e,
								rb.getString("titolErrorTancarConnexio"),
								JOptionPane.ERROR_MESSAGE);
			}
		}
		return seguir;
	}

}
